﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using Microsoft.AspNetCore.Http;
using Models.JsonResultModel;
using System.Linq;

namespace UtilsBase
{
    /// <summary>
    /// 字符串处理
    /// </summary>
    public class UtilsVisClient
    {
        public string getIP(HttpContext context)
        {//了获取最终客户端的真实 IP 地址，我们可以检查 Request.Headers 中是否包含 X-Forwarded-For 标头，并从该标头中提取最后一个 IP 地址：
            string ip = "";
            try
            {
                string forwardedHeader = "X-Forwarded-For";
                var remoteIpAddress = context.Connection.RemoteIpAddress;
                if (remoteIpAddress != null)
                {
                    ip = remoteIpAddress.ToString();
                    if (context.Request.Headers.ContainsKey(forwardedHeader))
                    {
                        ip = context.Request.Headers[forwardedHeader].ToString().Split(new char[] { ',' }).Last().Trim();
                    }
                    ip = ip.Split(new char[] { ':' }).Last().Trim();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return ip;
        }

    }


}


